import {
  type AxiosInstance,
  type AxiosRequestConfig,
  type AxiosResponse,
  type InternalAxiosRequestConfig,
} from "axios";
import axios from "axios";
import { ElMessage } from "element-plus";
import { userStore } from "@/stores/user";

const config = {
  baseURL: "http://localhost:8089",
  timeout: 5000,
  withCredentials: true,
};
export interface Result<T = any> {
  code: number;
  msg: string;
  data: T;
}
class Http {
  private instance: AxiosInstance;
  constructor(config: AxiosRequestConfig) {
    this.instance = axios.create(config);
    //配置请求拦截器
    this.interceptors();
  }
  private interceptors() {
    //请求拦截器
    this.instance.interceptors.request.use(
      (config: InternalAxiosRequestConfig) => {
        const store = userStore();
        //获取token
        let token = store.getToken;
        if (token) {
          config.headers!["token"] = token;
        }
        //处理请求
        return config;
      }
    ),
      (error: any) => {
        error.data = {};
        error.data.msg = "请求超时";
        return error;
      };
    //响应拦截器
    this.instance.interceptors.response.use((response: AxiosResponse) => {
      const res = response.data;
      if (res.code === 200) {
        return res;
      } else {
        ElMessage.error(res.msg || "接口报错");
        return Promise.reject(res);
      }
    }),
      (error: any) => {
        console.log("进入错误");
        error.data = {};
        if (error && error.response) {
          switch (error.response.status) {
            case 400:
              error.data.msg = "错误请求";
              ElMessage.error(error.data.msg);
              break;
            case 401:
              error.data.msg = "未授权，请重新登录";
              ElMessage.error(error.data.msg);
              break;
            case 403:
              error.data.msg = "拒绝访问";
              break;
            case 404:
              error.data.msg = "请求错误，未找到该资源";
              ElMessage.error(error.data.msg);
              break;
            case 405:
              error.data.msg = "请求方法未允许";
              ElMessage.error(error.data.msg);
              break;
            case 408:
              error.data.msg = "请求超时";
              ElMessage.error(error.data.msg);
              break;
            case 500:
              error.data.msg = "服务器端出错";
              ElMessage.error(error.data.msg);
              break;
            case 501:
              error.data.msg = "网络未实现";
              ElMessage.error(error.data.msg);
              break;
          }
        }
        return Promise.reject(error);
      };
  }
  //post请求
  post<T = Result>(url: string, data?: object): Promise<T> {
    return this.instance.post(url, data);
  }
  //put请求
  put<T = Result>(url: string, data?: object): Promise<T> {
    return this.instance.put(url, data);
  }
  //get请求
  get<T = Result>(url: string, params?: object): Promise<T> {
    return this.instance.get(url, { params });
  }
  //delete请求
  delete<T = Result>(url: string, params?: object): Promise<T> {
    return this.instance.delete(url, { params });
  }
  //文件上传
  upload<T = Result>(url: string, file: File): Promise<T> {
    let formData = new FormData();
    formData.append("file", file);
    return this.instance.post(url, formData, {
      headers: {
        "Content-Type": "multipart/form-data",
      },
    });
  }
}
export default new Http(config);
